import org.apache.commons.io.FileUtils;

import java.io.File;

public class CproThread extends Thread {
    /**
     * 原拷贝文件的大小
     */
    public Long orginSize;
    /**
     * 拷贝文件的大小
     */
    public Long targetSize;
    /**
     * 拷贝后的路径
     */
    public String target;

    // 通过构造方法给线程名字赋值
    public CproThread(String name, String resource, String target) {
        super(name);// 给线程名字赋值
        File resourceFile = new File(resource);
        this.orginSize = FileUtils.sizeOfDirectory(resourceFile);
        this.target = target;
    }

    public Long getFinalSize() {
        File targetFile = new File(this.target);
        return FileUtils.sizeOfDirectory(targetFile);
    }

    // 重写run方法，实现查看拷贝进度操作
    @Override
    public void run() {
        while ( (this.targetSize = this.getFinalSize()) < this.orginSize) {
            if ( Thread.currentThread().isInterrupted() ) {
                break;
            }
            try {
                Thread.sleep(1000);
                System.out.println(getName() + "当前进度:" + this.targetSize + "/" + this.orginSize);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
        System.out.println( Thread.currentThread().isInterrupted()? "查看拷贝线程已被终断" : "完成拷贝！");
    }
}
